// Enemy.cpp //
#include "Enemy.h"
#include "Player.h"
#include "Powerup.h"

//enemy class contains functions universal to all enimies
//specifically ability to remove enemies, and to make them
// explode or drop an item


vector<GameObject*> Enemy::EnemyList; // holds all enemies
Enemy::Enemy(string keyname) : GameObject(keyname) // constructor
{
    direction = Vector(0, 1); // all enemies initially move straight down
	explosionKeyname = "Explosion";

	EnemyList.push_back(this); // add new enimies to list
}

void Enemy::RemoveKilled() // removes dead enimies
{
    for(vector<GameObject*>::iterator Iter = EnemyList.begin(); Iter != EnemyList.end(); Iter += 0)
        if((*Iter)->dead)
		    Iter = EnemyList.erase(Iter);
		else
		    Iter++;
}

void Enemy::RemoveAll() // used when all enemies need to be erased
{
    for(vector<GameObject*>::iterator Iter = EnemyList.begin(); Iter != EnemyList.end(); Iter += 0)
		Iter = EnemyList.erase(Iter);
}

void Enemy::Explode() // controls what happens when an enemy is killed
{
	GameObject::Explode();
	
	Player::AwardPoints(pointValue);
	DropItem();
}

void Enemy::DropItem() // enemies can drop items
{
	if(rand() % 5 == 0)
	{
		int dropType = rand() % 5;
		Item* item;
		
		if(dropType == 0) // currently only health is available
		    item = new Powerup("ItemHealth");
		item->position = position;
	}
}
// enemies unique render, canbe overriden by specific enemy types
void Enemy::Render(SDL_Surface* screen) 
{
	char buffer[4];
	sprintf(buffer, "%d", this->numValue); // get the num value
	sprite->Render(screen,position); // render sprite
	sprite->texture->DrawText(screen,buffer,16,this->position.X,this->position.Y,250,250,250,true);//then number
}


